<?php
    $hashid = arrGet($_POST,"hashid");
    
    # Hae data
    $workout = API::workoutLoad(array(
        "hashid" => $hashid
    ));
?>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="row">
            <div class="col-sm-4">
                <strong>Training program</strong>
            </div>
            <div class="col-sm-8">
                <?php
                    $tpHashid = htmlescape(arrGet($workout,"training_program_hashid"));
                    $tpName = htmlescape(arrGet($workout,"training_program_name"));
                    if ($tpHashid) {
                ?>
                        <a class="a-subview-dialog"
                          data-ui-view="tplist"
                          data-ui-subview="info"
                          data-ui-title="<?php echo $tpName; ?>"
                          data-hashid="<?php echo $tpHashid; ?>"
                          href="#">
                            <?php echo $tpName; ?>
                        </a>
                <?php
                    } else {
                        echo "&nbsp;";
                    }
                ?>
            </div>
        </div>
        
        <div class="row">
            <div class="col-sm-4">
                <strong>Template</strong>
            </div>
            <div class="col-sm-8">
                <?php
                    $templateHashid = htmlescape(arrGet($workout,"templatehashid"));
                    $extratext = htmlescape(arrGet($workout,"template_extratext"));
                    if ($templateHashid) {
                ?>
                        <a class="a-subview-dialog"
                          data-ui-view="templatelist"
                          data-ui-subview="info"
                          data-ui-title="<?php echo $extratext; ?>"
                          data-hashid="<?php echo $templateHashid; ?>"
                          href="#">
                            <?php echo $extratext; ?>
                        </a>
                <?php
                    } else {
                        echo "&nbsp;";
                    }
                ?>
            </div>
        </div>
        
        <div class="row">    
            <div class="col-sm-4">
                <strong>Location</strong>
            </div>
            <div class="col-sm-8">
                <?php
                    echo htmlescape(arrGet($workout,"locationtext"));
                ?>
            </div>
        </div>
        
        <div class="row">    
            <div class="col-sm-4">
                <strong>Starttime</strong>
            </div>
            <div class="col-sm-8">
                <?php
                    $starttime = strtotime(arrGet($workout,"starttime"));
                    echo date("Y-m-d H:i",$starttime);
                ?>
            </div>
        </div>
        
        <div class="row">            
            <div class="col-sm-4">
                <strong>Endtime</strong>
            </div>
            <div class="col-sm-8">
                <?php
                    $endtime = strtotime(arrGet($workout,"endtime"));
                    echo date("Y-m-d H:i",$endtime);
                ?>
            </div>
        </div>
        
        <div class="row">
            <div class="col-sm-4">
                <strong>Duration (min)</strong>
            </div>
            <div class="col-sm-8">
                <?php
                    $duration = floor(($endtime-$starttime)/60);
                    echo $duration;
                ?>
            </div>
        </div>
        
        <div class="row">
            <div class="col-sm-4">
                &nbsp;
            </div>
            <div class="col-sm-8">
                <a class="a-navigation"
                  data-ui-view="editworkout"
                  data-hashid="<?php echo htmlescape($hashid); ?>"
                  href="#">
                    Edit workout
                </a>
            </div>
        </div>
    </div>
</div>

<?php
    $records = (array)arrGet($workout,"records");
    if (count($records) > 0) {
    ?>
        <div class="panel panel-default">
            <div class="panel-body">
                <strong>Notes</strong>
                <p>
                    <?php
                        $record = arrGet($records[0],"record");
                        $record = htmlescape(trim($record));
                        echo nl2br($record);
                    ?>
                </p>
            </div>
        </div>
    <?php
    }
?>

<table class="table table-striped">
    <thead>
        <tr>
            <th>
                #
            </th>
            <th>
                Name/desc
            </th>
            <th>
                Special&nbsp;/&nbsp;gear
            </th>            
            <th>
                Reps&nbsp;x&nbsp;weight&nbsp;/&nbsp;rest
            </th>
        </tr>
    </thead>
    
    <tbody>
        <?php
            $i = 1;
            $exercises = (array)arrGet($workout,"exercises");
            foreach ($exercises as $exercise) {
            ?>
                <tr>
                    <td>
                        <div>
                            <?php echo $i; ?>
                        </div>
                    </td>
                    <td>
                        <div>
                            <?php echo htmlescape($exercise["extratext"]); ?>
                        </div>
                    </td>
                    <td>
                        <?php                            
                            # Erikoistekniikka
                            echo "<div>".
                                htmlescape(getSpecialExerciseDesc($exercise["special"])).
                                "</div>";
                            
                            # Varusteet
                            foreach (getGearDesc() as $gear => $desc) {
                                if ($exercise[$gear] == 1) {
                                    echo "<div class=\"margin-left\">".
                                        "+&nbsp;".
                                        htmlescape($desc).
                                        "</div>";
                                }
                            }
                        ?>
                    </td>
                    <td>
                        <?php
                            $exerciseid = null;
                            $barbellType = null;
                            $sets = (array)arrGet($exercise,"sets");
                            foreach ($sets as $set) {
                                $category           = htmlescape($set["category"]);
                                $exerciseText       = htmlescape($set["exercisetext"]);
                                $musclegroup        = htmlescape($set["musclegroup"]);
                                $repTypeDesc        = getRepetitionsTypeDesc($set["repetitions_type"]);
                                $barbellTypeDesc    = "";
                                
                                # Onko levytangotyyppiä asetettu
                                if ($set["barbell_type"]) {
                                    $barbellTypeDesc = "&nbsp;(".
                                        htmlescape(getBarbellTypeDesc($set["barbell_type"])).
                                        ")";
                                }
                                
                                echo(
                                    # Harjoite
                                    (($exerciseid != $set["exerciseid"] || $barbellType != $set["barbell_type"])
                                        ?
                                            "<div>".
                                                $musclegroup."&nbsp;-&nbsp;".$category."<br />".
                                                $exerciseText.$barbellTypeDesc.
                                            "</div>"
                                        : ""
                                    ).
                                    
                                    # Toistot
                                    "<div class=\"margin-left\">".
                                    "*&nbsp;".
                                    htmlescape($set["repetitions"]).
                                    
                                    # Kertaa
                                    "&nbsp;x&nbsp;".
                                    
                                    # Paino
                                    htmlescape($set["weight"]).
                                    
                                    # Yksikkö
                                    "&nbsp;".
                                    htmlescape($exercise["unit"]).
                                    
                                    # Kautta lepotauko
                                    "&nbsp;/&nbsp;".
                                    htmlescape(secToStr($set["rest_interval_sec"])).
                                    
                                    # Toistotyyppi
                                    (($set["repetitions_type"] != "normal")
                                        ? "&nbsp;<strong>".htmlescape($repTypeDesc)."</strong>"
                                        : ""
                                    ).
                                    
                                    "</div>"
                                );
                                
                                $exerciseid = $set["exerciseid"];
                                $barbellType = $set["barbell_type"]; 
                            }
                        ?>
                    </td>
                </tr>
            <?php
                $i++;
            }
        ?>
    </tbody>
</table>